/*** 
This do-file executes all the scripts to create the Low-Wage employment ZIP-level
maps. 
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
local build_running = r(buildrunning)
if (`build_running' == 0) include "${root}/code/config_interactive.do"

* Set globals
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/data/derived"
cap mkdir "${root}/data/derived/R packages"
cap mkdir "${root}/data/derived/Shapefiles"
cap mkdir "${root}/results/Employment"

*-------------------------------------------------------------------------------
* Create input data for maps 
*-------------------------------------------------------------------------------

project, do("${root}/code/figs/Employment/Employment - Change in Low-Wage Employment Maps.do")

*-------------------------------------------------------------------------------
* Unzip all R packages in DVC for maps 
*-------------------------------------------------------------------------------
cd "${root}/data/derived/R packages"

local r_packages: dir "${root}/data/dvc/R packages" files "*.zip"

foreach package of local r_packages {
	project, uses("${root}/data/dvc/R packages/`package'")
	unzipfile "${root}/data/dvc/R packages/`package'", replace
}

*-------------------------------------------------------------------------------
* Unzip shapefiles in DVC for maps
*-------------------------------------------------------------------------------
cd "${root}/data/derived/Shapefiles"

project, uses("${root}/data/dvc/Shapefiles/gz_2010_us_860_00_500k.zip")
unzipfile "${root}/data/dvc/Shapefiles/gz_2010_us_860_00_500k.zip", replace

*-------------------------------------------------------------------------------
* Execute R code to create maps
*-------------------------------------------------------------------------------
 
project, uses("${root}/code/figs/Employment/Employment - Change in Low-Wage Employment Maps.R")
project, uses("${root}/data/derived/Employment/Map - Change in Low-Income Employment, ZIP.csv")

* First arg: code location; second arg: whether the build is running; third arg: root directory

shell R < "${root}/code/figs/Employment/Employment - Change in Low-Wage Employment Maps.R" "`build_running'" "${root}" --no-save

* Inform the project that the maps have been created
project, creates("${root}/results/Employment/map_employment_byZIP_Chicago.png")
project, creates("${root}/results/Employment/map_employment_byZIP_NYC.png")
project, creates("${root}/results/Employment/map_employment_byZIP_SF.png")
